<template>
    <div>
        <navigationBar navigationTitle="动态" />
        <view class="sticky">
            <view class="wrap-search flex align-center just-center">
                <view class="addDynamic" @click="onAdd"> + 发布动态 </view>

                <view class="search-bg flex align-center">
                    <image
                        class="img"
                        :src="getImageURL('technician/choose/search.png')"
                    />
                    <view class="line"></view>

                    <input
                        class="input"
                        type="text"
                        placeholder="请输入服务人姓名"
                        v-model="keywords"
                        @blur="onBlur"
                    />

                    <view class="search" v-if="keywords" @click="search">
                        搜索
                    </view>
                </view>
            </view>

            <view class="wrap-order-status flex" :style="'top: 0px;'">
                <block v-for="(item, index) in tab_list" :key="index">
                    <view
                        class="status-item flex align-center just-center"
                        @click="onTab(index)"
                    >
                        <view :class="{ active: item.status === status }">
                            {{ item.title }}
                        </view>
                    </view>
                </block>
            </view>
            <scroll-view
                class="u-flex claWrap"
                scroll-x
                :scroll-into-view="scrollID"
                @touchmove.stop
            >
                <block v-for="(item, index) in tab_lists" :key="index">
                    <view
                        class="claItem flex align-center just-center"
                        @click="onTabstatusID(index, item.id)"
                        :class="{ active: item.id === statusID }"
                        :id="`tab_${index + 1}`"
                    >
                        <view class="cla_text u-flex-center">
                            {{ item.name }}
                        </view>
                    </view>
                </block>
            </scroll-view>
        </view>

        <view class="content">
            <block v-for="(item, index) in dynamicList" :key="index">
                <view class="card" @click="onDetail(item)">
                    <image :src="item.image" mode="scaleToFill" />
                    <view class="title">
                        {{ item.name }}
                    </view>
                    <view class="flex align-center just-between technician">
                        <view class="name flex align-center">
                            <image
                                class="img"
                                :src="item.technician.work_image"
                                mode="scaleToFill"
                            />
                            {{
                                item.technician.name.length > 5
                                    ? item.technician.name.substring(0, 5) +
                                      "..."
                                    : item.technician.name
                            }}
                        </view>
                        <view class="distance flex align-center">
                            <image
                                class="icon"
                                src="/static/static/technician/order/location.png"
                                mode="scaleToFill"
                            />
                            {{ item.distance || 0 }}km
                        </view>
                    </view>
                </view>
            </block>

            <emptyData v-if="dynamicList.length == 0" :text="'暂无数据'" />
        </view>

        <view class="mark" v-if="showCard" @click="onClose"></view>
        <view
            class="bottomCard card flex column align-center just-around"
            v-if="showCard"
        >
            <view class="items" @click="onAddDynamic('photo')"> 发布图片 </view>
            <view class="items" @click="onAddDynamic('video')"> 发布视频 </view>
            <view class="items cancel" @click="onClose"> 取消 </view>
        </view>
        <tabbar :cur="3"></tabbar>
    </div>
</template>

<script>
import tabbar from "@/components/tabbar.vue";

import { getDynamicList, getCategoryList } from "@/API/API/recruit.js";
export default {
    components: {
        tabbar,
    },
    data() {
        return {
            keywords: "",
            tab_list: [
                { title: "推荐", status: 1 },
                { title: "关注", status: 2 },
            ],
            tab_lists: [],
            status: 1,

            cateid: "tab_0",
            scrollID: "tab_0",
            statusID: 0,
            dynamicList: [],
            lat: 0,
            lng: 0,
            showCard: false,
            page: 1,
        };
    },

    onPullDownRefresh() {
        this.onRefresh();
    },

    onLoad(options) {
        uni.showLoading({
            title: "加载中",
            mask: true,
        });
        this.getTabList();
    },

    onReachBottom() {
        if (this.dynamicList.length < this.total) {
            this.page++;
            this.getList();
        }
    },

    methods: {
        onBlur() {
            if (this.keywords == "") {
                this.page = 1;
                this.dynamicList = [];
                this.getList();
            }
        },
        onClose() {
            this.showCard = false;
        },
        onRefresh() {
            this.page = 1;
            this.dynamicList = [];
            this.getList();
        },
        onTab(index) {
            this.status = this.tab_list[index].status;
            this.onRefresh();
        },
        onTabstatusID(index, id) {
            this.statusID = id;
            this.cateid = `tab_${index + 1}`;
            this.scrollID = this.cateid;
            this.onRefresh();
        },
        // 分类
        getTabList() {
            getDynamicList({ type: 1 }).then((res) => {
                res.data.unshift({ name: "全部", id: 0 });
                this.tab_lists = res.data;
            });
            uni.getLocation({
                type: "gcj02",
                success: (res) => {
                    uni.hideLoading();
                    this.lat = res.latitude;
                    this.lng = res.longitude;
                    this.getList();
                },
                fail: () => {
                    uni.hideLoading();
                    uni.showToast({
                        title: "获取定位失败",
                        icon: "none",
                    });
                    setTimeout(() => {
                        this.lat = 45.785735;
                        this.lng = 126.687165;
                        this.getList();
                    }, 800);
                },
            });
        },
        // 列表
        getList() {
            getCategoryList({
                page: this.page,
                type: this.status,
                cate_id: this.statusID,
                keyword: this.keywords,
                lat: this.lat,
                lng: this.lng,
            }).then((res) => {
                this.dynamicList.push(...res.data.list.data);
                this.total = res.data.list.total;
            });
        },
        // 详情
        onDetail(item) {
            uni.navigateTo({
                url: `/subPages/technician/recruit/dynamicDetails?dynamic_id=${item.id}&lat=${this.lat}&lng=${this.lng}`,
            });
        },
        // 搜索
        search() {
            this.page = 1;
            this.dynamicList = [];
            this.getList();
        },
        // 添加动态
        onAdd() {
            this.showCard = true;
        },
        onAddDynamic(type) {
            uni.navigateTo({
                url: `/subPages/technician/recruit/addDynamic?type=${type}`,
            });
            this.showCard = false;
        },
    },
};
</script>
<style lang="scss" scoped>
.wrap-search {
    position: sticky;
    left: 0;
    width: 100%;
    height: 100rpx;
    background-color: white;
    padding: 0 20rpx;
    box-sizing: border-box;
    overflow: hidden;

    .addDynamic {
        width: 25%;
        text-align: center;
        border: 1rpx solid $theme-color;
        height: 55rpx;
        line-height: 55rpx;
        color: $theme-color;
        border-radius: 30rpx;
        overflow: hidden;
        margin-right: 10rpx;
    }

    .search-bg {
        flex: 1;
        height: 60rpx;
        background-color: $bg-color;
        border-radius: 20rpx;
        overflow: hidden;

        .img {
            margin-left: 22rpx;
            width: 27rpx;
            height: 25rpx;
        }
        .line {
            margin-left: 14rpx;
            width: 2rpx;
            height: 32rpx;
            background-color: #b0b0b0;
        }
        .input {
            font-size: 26rpx;
            margin-left: 14rpx;
            flex: 1;
            margin-right: 20rpx;
            color: $title-color;
        }
        .search {
            width: 100rpx;
            height: 50rpx;
            line-height: 50rpx;
            text-align: center;
            color: $bg-text-color;
            background: $grad-theme-color;
            border-radius: 30rpx;
            overflow: hidden;
            margin-right: 10rpx;
        }
        .place {
            color: #b0b0b0;
        }
    }
}
.wrap-order-status {
    position: sticky;
    left: 0;
    width: 100%;
    overflow: hidden;
    height: 100rpx;
    background: white;
    flex-wrap: wrap;

    .status-item {
        flex: 1;
        height: 100rpx;
        overflow: hidden;

        > view {
            display: inline-block;
            height: 100rpx !important;
            line-height: 100rpx;
            box-sizing: border-box;
            border-bottom: 6rpx solid white;
        }

        .active {
            border-color: $theme-color;
        }
    }
}
/deep/.uni-scroll-view-content {
    display: flex;
    height: 100rpx;
    background: white;
}
.claWrap {
    overflow-x: auto;
    overflow-y: hidden;
    transition: all 0.3s;

    .claItem {
        padding: 0 20rpx;
        word-break: keep-all;
        border-bottom: 6rpx solid white;

        .cla_text {
            margin: 0 14rpx;
            height: 32rpx;
            border-radius: 40rpx;
            text-align: center;
            line-height: 32rpx;
        }
    }
    .active {
        border-color: $theme-color;
    }

    .claCss {
        .cla_text {
            color: $theme-color;
        }
    }
}
.sticky {
    position: sticky;
    top: 0;
    left: 0;
    background: white;
    z-index: 9;
}

.content {
    padding: 10rpx 20rpx 140rpx 20rpx;
    box-sizing: border-box;
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .card {
        width: calc(50% - 20rpx);
        background: #fff;
        border-radius: 20rpx;
        margin: 10rpx;
        padding-bottom: 20rpx;
        box-sizing: border-box;
        image {
            width: 100%;
            height: 340rpx;
        }

        .title {
            margin: 0 14rpx;
            padding: 20rpx 0;
            width: 100%;
            font-size: 28rpx;
            color: #333333;
            // 单行省略
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .technician {
            margin: 0 14rpx;
            font-size: 22rpx;
        }
        image {
            width: 100%;
            border-radius: 20rpx 20rpx 0 0;
        }
        .img {
            width: 32rpx;
            height: 32rpx;
            margin-right: 10rpx;
        }
        .icon {
            margin-right: 10rpx;
            width: 20rpx;
            height: 22rpx;
        }
    }
}
.mark {
    width: 100vw;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;
    background: rgba(0, 0, 0, 0.5);
}
.bottomCard {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 482rpx;
    z-index: 99999;
    background-color: #f6f6f6;
    border-radius: 20rpx 20rpx 0 0;
    .items {
        width: 710rpx;
        height: 114rpx;
        text-align: center;
        line-height: 114rpx;
        border-radius: 10rpx 10rpx 10rpx 10rpx;
        background: #ffffff;
    }
    .items:active {
        opacity: 0.6;
    }
    .cancel {
        color: $theme-color;
    }
}
</style>